################################################################################
# Gendered Perceptions of Legislative Influence ################################
# Kaslovsky, Koch, and Olson ###################################################
# This file makes our descriptive plots of the North Carolina Legislature ######
################################################################################

## load library
library(ggplot2)
library(dplyr)

##set the working directory
setwd("Gendered Perceptions/Data")

######################### PROFFESIONALIZATION ###############################################
##load in professionalization data and remove NAs

prof <- read.csv("squire.csv")
prof<- na.omit(prof)
prof <- prof[is.element(prof$X2021.state,state.abb),]

colnames(prof)
##create an average professionalization score across all years in the data
prof$ProfAvg <- rowMeans(prof[, c("Prof.1996", "Prof.2003", "Prof.2009", "Prof.2015")])

## create colors and order from least to greatest
colors <- ifelse(prof$`X2021.state` == "NC", "gray2", "gray")
prof$`X2021.state` <- factor(prof$`X2021.state`, levels = unique(prof$`X2021.state`)[order(prof$ProfAvg)])

# Create a bar plot 
ggplot(prof, aes(x = `X2021.state`, y = ProfAvg, fill = colors)) +
  geom_bar(stat = "identity") +
  labs(x = "State", y = "Professionalization") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        plot.title = element_text(hjust = 0.5)) +
  scale_fill_identity() 

ggsave("../figures_tables/fig1b.pdf",width=6,height=4)  

mean(prof$ProfAvg) # Avg = 0.204515, NC = 0.19525

############### COMPETITION ##############################################

# load data (9/18 release)      

  kl <- read.csv("klarner_transformed.csv")

  colors <- ifelse(kl$st == "NC", "gray2", "gray")

##plot the densities
  
  kl$st <- factor(kl$st,levels=kl$st[order(kl$mean_margin)])
  
  ggplot(kl, aes(x = st, y = mean_margin, fill = colors)) +
    geom_bar(stat = "identity") +
    labs(x = "State", y = "Mean Margin of Victory") +
    theme_minimal()+
    theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
          plot.title = element_text(hjust = 0.5)) +
    scale_fill_identity() 
  
  ggsave("../figures_tables/fig1c.pdf",width=6,height=4)  
  
################################# PERCENT WOMEN ##################################################

# load in SLES data that's been limited to the appropriate time period
women_dta<- read.csv("women_sles.csv")

##calculate percent female for each state/year
perc_female <- women_dta %>%
  group_by(state, year_elected) %>%
  summarise(perc_female = mean(female == 1, na.rm = TRUE) * 100)

## now take the average across all years
avg_perc_female <- perc_female[!is.na(perc_female$perc_female),] %>%
  group_by(state) %>%
  summarise(avg_perc_female = mean(perc_female, na.rm = TRUE))

## create colors and order from least to greatest
colors1 <- ifelse(avg_perc_female$state == "NC", "gray2", "gray")
avg_perc_female$state <- factor(avg_perc_female$state, levels = unique(avg_perc_female$state)[order(avg_perc_female$avg_perc_female)])

# Create a bar plot 
ggplot(avg_perc_female, aes(x = state, y = avg_perc_female, fill = colors1)) +
  geom_bar(stat = "identity") +
  labs(x = "State", y = "Percent Female") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        plot.title = element_text(hjust = 0.5)) +
  scale_fill_identity() 

ggsave("../figures_tables/fig1d.pdf",width=6,height=4)  

mean(avg_perc_female$avg_perc_female) # Avg = 22.74248, NC = 20.8

################################# LEGISLATURE SIZE ##################################################

##load in dubin data
size<- read.csv("size.csv")
size$size <- size$lht+size$uht

## now take the average across all years
size <- size %>%
  group_by(state) %>%
  summarise(size = mean(size, na.rm = TRUE))

## create colors and order from least to greatest
size <- size[size$state!="NE",]
colors1 <- ifelse(size$state == "NC", "gray2", "gray")
size$state <- factor(size$state, levels = unique(size$state)[order(size$size)])

# Create a bar plot 
ggplot(size, aes(x = state, y = size, fill = colors1)) +
  geom_bar(stat = "identity") +
  labs(x = "State", y = "Legislature Size (House + Senate)") +
  theme_minimal()+
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        plot.title = element_text(hjust = 0.5)) +
  scale_fill_identity() 

ggsave("../figures_tables/fig1a.pdf",width=6,height=4)  



